﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Web.Question;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.ViewQuery;
using PowerCreator.LMS.Common.Script;

public partial class sysadmin_QuestionManager_ManagerPaperInfo : PowerCreator.LMS.Web.Base.BasePage
{   
    ManagerPaper objManagerPaper = new ManagerPaper();
    Course objCourse = new Course();
    ManagerQuestion objManagerQuestion = new ManagerQuestion();
    ViewQuery objViewQuery = new ViewQuery();
    PowerCreator.LMS.Web.Project.ProjectResource objProjectResource = new PowerCreator.LMS.Web.Project.ProjectResource();
    

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!GetRole(Role.ManagerPaper))
        //{
        //    Script.ShowAndHistory("您没有管理试卷的权限!", -1);
        //    return;
        //}
        if (!IsPostBack)
        {
            InitPage();
            ViewState["SortOrder"] = "PaperID";
            ViewState["OrderDire"] = "Desc";
            ViewState["SearchType"] = "0";  //0简单搜索1高级搜索
            string search = GetSearch();
            BinderPager(search);
            BinderGV(search, 1);
           
            
        }
    }
    protected override void InitPage()
    {
        //btn_Add.Text = GetLanageValueByKey("SysPaperInfoAddin", PowerCreator.LMS.Language.LanagetItemType.Page);
       
        btnDel.Text = GetLanageValueByKey("btn_Course_btnDel", PowerCreator.LMS.Language.LanagetItemType.Page);

        btnDel.Attributes.Add("onClick", "javascript:return confirm('" + GetLanageValueByKey("lbl_OnlineUser_DelMsg", PowerCreator.LMS.Language.LanagetItemType.Page) + "');");

        lab_TitleInfo.Text = GetLanageValueByKey("lab_ManagerPaperInfo_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_SelectType.Text = GetLanageValueByKey("lab_AddPaperInfo_PaperSelectType", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_PaperType.Text = GetLanageValueByKey("lab_AddPaperInfo_PaperType", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_PaperName.Text = GetLanageValueByKey("lab_AddPaperInfo_PaperName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_PaperState.Text = GetLanageValueByKey("lab_ManagerPaperInfo_PaperState", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_SortSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);

        chkAll.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxCancel", PowerCreator.LMS.Language.LanagetItemType.Page);

        dpdlst_SelectType.Items[0].Text = GetLanageValueByKey("Msg_ManagerQuestion_PleaseCheck", PowerCreator.LMS.Language.LanagetItemType.Page);
        dpdlst_SelectType.Items[1].Text = GetLanageValueByKey("Msg_AddPaperInfo_SelectQuestionType1", PowerCreator.LMS.Language.LanagetItemType.Page);
        dpdlst_SelectType.Items[2].Text = GetLanageValueByKey("Msg_AddPaperInfo_SelectQuestionType2", PowerCreator.LMS.Language.LanagetItemType.Page);

        dpdlst_PaperType.Items[0].Text = GetLanageValueByKey("Msg_ManagerQuestion_PleaseCheck", PowerCreator.LMS.Language.LanagetItemType.Page);
        dpdlst_PaperType.Items[1].Text = GetLanageValueByKey("lab_AddPaperInfo_CommonPaper", PowerCreator.LMS.Language.LanagetItemType.Page);
        dpdlst_PaperType.Items[2].Text = GetLanageValueByKey("lab_AddPaperInfo_TestPaper", PowerCreator.LMS.Language.LanagetItemType.Page);

        dpdlst_PaperState.Items[0].Text = GetLanageValueByKey("Msg_ManagerQuestion_PleaseCheck", PowerCreator.LMS.Language.LanagetItemType.Page);
        dpdlst_PaperState.Items[1].Text = GetLanageValueByKey("Msg_ManagerPaperInfo_NoBuild", PowerCreator.LMS.Language.LanagetItemType.Page);
        dpdlst_PaperState.Items[2].Text = GetLanageValueByKey("Msg_ManagerPaperInfo_Build", PowerCreator.LMS.Language.LanagetItemType.Page);


        base.InitPage();
    }
    private string GetSearch()
    {
        StringBuilder str_search = new StringBuilder();
        //str_search.Append(" 1=1 and PaperBuildState=1  ");
        str_search.Append(" 1=1 ");
        if (ViewState["SearchType"].ToString() == "1")
        {

            if (dpdlst_PaperType.SelectedValue != "-1")
            {
                str_search.Append(" and PaperType=" + dpdlst_PaperType.SelectedValue);
            }

            if (dpdlst_SelectType.SelectedValue != "-1")
            {
                str_search.Append(" and PaperSelectType=" + dpdlst_SelectType.SelectedValue);
            }

            if (dpdlst_PaperState.SelectedValue != "-1")
            {
                str_search.Append(" and PaperBuildState=" + dpdlst_PaperState.SelectedValue);
            }

            if (txtPaperName.Text.Trim() != "")
            {
                str_search.Append(" and PaperName like '%" + txtPaperName.Text + "%'");
            }
        }
        else
        {
            if (txt_SearchKey.Text.Trim() != "")
            {
                str_search.Append(" and PaperName like '%" + txt_SearchKey.Text + "%'");
            }
        }


        return str_search.ToString();
    }
    private void BinderPager(string search)
    {
        AspNetPager1.RecordCount = objManagerPaper.CountPaper(search);
        AspNetPager1.PageSize = LMSPageSize;
    }

    private void BinderGV(string search, int pageIndex)
    {
        DataTable dt = new DataTable();
        string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
        dt = objManagerPaper.QueryPaper(search, sort,LMSPageSize, pageIndex);
        DataColumn dc = new DataColumn("SelectTypeNew");
        dt.Columns.Add("url");
        dt.Columns.Add(dc);
        dt.Columns.Add("Index");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["url"]=dt.Rows[i]["PaperID"].ToString()+"$"+dt.Rows[i]["PaperSelectType"].ToString();
            if (dt.Rows[i]["PaperSelectType"].ToString() == "1")
            {
                dt.Rows[i]["SelectTypeNew"] = GetLanageValueByKey("Msg_AddPaperInfo_SelectQuestionType1", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
            //else if (dt.Rows[i]["PaperSelectType"].ToString() == "2")
            //{
            //    dt.Rows[i]["SelectTypeNew"] = "自动选题";
            //}
            else
            {
                dt.Rows[i]["SelectTypeNew"] = GetLanageValueByKey("Msg_AddPaperInfo_SelectQuestionType2", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
            dt.Rows[i]["Index"] = (pageIndex-1)*10+i + 1;
        }

        gvPaperList.Columns[3].HeaderText = GetLanageValueByKey("lab_AddPaperInfo_PaperName", PowerCreator.LMS.Language.LanagetItemType.Page);

        gvPaperList.Columns[4].HeaderText = GetLanageValueByKey("lab_AddPaperInfo_PaperTestTime", PowerCreator.LMS.Language.LanagetItemType.Page);
        gvPaperList.Columns[5].HeaderText = GetLanageValueByKey("lab_BuilderPaper_PaperCountScore", PowerCreator.LMS.Language.LanagetItemType.Page);
        gvPaperList.Columns[6].HeaderText = GetLanageValueByKey("lab_AddPaperInfo_PaperDefaultPassScore", PowerCreator.LMS.Language.LanagetItemType.Page);
        gvPaperList.Columns[7].HeaderText = GetLanageValueByKey("lab_AddPaperInfo_PaperType", PowerCreator.LMS.Language.LanagetItemType.Page);
        gvPaperList.Columns[8].HeaderText = GetLanageValueByKey("lab_AddPaperInfo_PaperSelectType", PowerCreator.LMS.Language.LanagetItemType.Page);
        gvPaperList.Columns[9].HeaderText = GetLanageValueByKey("lab_ManagerPaperInfo_PaperState", PowerCreator.LMS.Language.LanagetItemType.Page);
        gvPaperList.Columns[10].HeaderText = GetLanageValueByKey("btn_ManagerQuestion_Work", PowerCreator.LMS.Language.LanagetItemType.Page);

           

        gvPaperList.DataSource = dt;
        gvPaperList.DataBind();

       
    
        PreAspNetPager(AspNetPager1);
    }
    
    protected void gvPaperList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "mypreview")
        {
            string url = string.Empty;
            string str = e.CommandArgument.ToString();
            string[] strlist = str.Split('$');
            if (strlist[1].ToString() == "2")
            {
                url = "ShowPaperPloy.aspx?p=" + strlist[0].ToString() + "&py=" + strlist[1].ToString();
            }
            else
            {
                url = "ShowPaperInfo.aspx?p=" + strlist[0].ToString();
            }
            
            Script.NewOpenURL(url);
        }
        else if (e.CommandName.ToLower() == "myeditor")
        {
            string str = e.CommandArgument.ToString();
            string[] strlist = str.Split('$');
            string url = "EditPaperInfo.aspx?p=" + strlist[0].ToString() + "&py=" + strlist[1].ToString();
            Script.RedirectURL(url);
        }
        else if (e.CommandName.ToLower() == "mydelete")
        {
            int paperID = int.Parse(e.CommandArgument.ToString());//获取试卷ID

            //检查是否有方案引用
            if (objProjectResource.QueryProjectResourceCount(" FKProresPaperID=" + paperID) > 0)
            {
                Script.Alert(this, "msg", "该试卷被方案引用，请先删除引用该试卷的方案");
                return;
            }

            if (dpdlst_PaperType.SelectedValue == "0")
            {
                if (!CheckPaper_Del(paperID))
                {
                    return;
                }
            }

            #region 删除试卷信息表和试卷试题表相关记录

            objManagerPaper.DeletePaper(paperID);                               //删除试卷信息表
            string cmd = "FKPaperQuestionPaperID=" + paperID.ToString();
            DataTable dt_pqinfo = new DataTable();
            dt_pqinfo = objManagerPaper.QueryPaperQuestion(cmd);                       //获取试卷试题信息表相关记录
            for (int i = 0; i < dt_pqinfo.Rows.Count; i++)
            {
                int PaperQuestionID = int.Parse(dt_pqinfo.Rows[i]["PaperQuestionID"].ToString());
                objManagerPaper.DeletePaperQuestion(PaperQuestionID);                      //删除试卷试题信息表记录
            }
            #endregion

            #region 删除用户试卷答案表相关记录
            DataTable dt_paperuseranswer = new DataTable();
            dt_paperuseranswer = objManagerPaper.QueryPaperUserAnswer("FKPUanswerPaperID=" + paperID.ToString());
            for (int i = 0; i < dt_paperuseranswer.Rows.Count; i++)
            {
                int PUanswerID = int.Parse(dt_paperuseranswer.Rows[i]["PUanswerID"].ToString());
                objManagerPaper.DeletePaperUserAnswer(PUanswerID);
            }
            #endregion

            //#region 删除用户试卷考试表相关记录
            //DataTable dt_userpaperhistory = new DataTable();
            //dt_userpaperhistory = objuserpaperhistoryinfo.Query("PaperID=" + paperID.ToString());
            //for (int i = 0; i < dt_userpaperhistory.Rows.Count; i++)
            //{
            //    int PUAID = int.Parse(dt_userpaperhistory.Rows[i]["UPHID"].ToString());
            //    objuserpaperhistoryinfo.Delete(PUAID);
            //}
            //#endregion

            string search = GetSearch();
            BinderPager(search);
            BinderGV(search, AspNetPager1.CurrentPageIndex);
        }
        else if (e.CommandName == "btn_Out")
        {
            int paperID = int.Parse(e.CommandArgument.ToString());//获取试卷ID
            VOPaper pinfo = new VOPaper();

            pinfo = objManagerPaper.LoadPaper(paperID);
            if (pinfo.PaperSelectType == 2)
            {
                if (string.IsNullOrEmpty(pinfo.PaperCelueID.ToString()))
                {
                    Script.Alert(Page, "show",  GetLanageValueByKey("Msg_ManagerPaperInfo_msg1", PowerCreator.LMS.Language.LanagetItemType.Page));
                    return;
                }
            }
            //WordManager objWordManager = new WordManager();
            //objWordManager.outword(this, paperID);
            objManagerPaper.GetOutWord(this, paperID);
        }
        else if (e.CommandName == "btn_SetUser")
        {
            string url = "SetJoinTestUser.aspx?pID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
    }
    #region 删除试卷时检查是否有方案引用
    private bool CheckPaper_Del(int paperID)
    {
        //string search = "TypeID=1 and ResourceID=" + paperID;
        //DataTable dt_ProjectResource = objManagerProjectResource.Query(search);
        //if (dt_ProjectResource.Rows.Count > 0)
        //{
        //    Script.Alert(this,"","提示："+dt_ProjectResource.Rows[0]["ProjectName"].ToString()+"方案引用该试卷");
        //    return false;
        //}
        return true;
 
    }

    #endregion
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "1";
        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, AspNetPager1.CurrentPageIndex);
    }
    protected void gvPaperList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.Header)
        //{
        //    e.Row.Cells[3].Text = GetLanageValueByKey("lab_AddPaperInfo_PaperName", PowerCreator.LMS.Language.LanagetItemType.Page);

        //    e.Row.Cells[4].Text = GetLanageValueByKey("lab_AddPaperInfo_PaperTestTime", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    e.Row.Cells[5].Text = GetLanageValueByKey("lab_BuilderPaper_PaperCountScore", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    e.Row.Cells[6].Text = GetLanageValueByKey("lab_AddPaperInfo_PaperDefaultPassScore", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    e.Row.Cells[7].Text = GetLanageValueByKey("lab_AddPaperInfo_PaperType", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    e.Row.Cells[8].Text = GetLanageValueByKey("lab_AddPaperInfo_PaperSelectType", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    e.Row.Cells[9].Text = GetLanageValueByKey("lab_ManagerPaperInfo_PaperState", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    e.Row.Cells[10].Text = GetLanageValueByKey("btn_ManagerQuestion_Work", PowerCreator.LMS.Language.LanagetItemType.Page);
            
        //    //e.Row.Cells[8].Text = GetLanageValueByKey("btn_ManagerQuestion_QuestionState", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    //e.Row.Cells[9].Text = GetLanageValueByKey("btn_ManagerQuestion_Work", PowerCreator.LMS.Language.LanagetItemType.Page);

        //}
        
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton ibtnPreview = (LinkButton)e.Row.Cells[8].FindControl("ibtnPreview");
            LinkButton btn_Out = (LinkButton)e.Row.Cells[8].FindControl("btn_Out");
            LinkButton ibtnEditor = (LinkButton)e.Row.Cells[10].FindControl("ibtnEditor");

            LinkButton ibtnDelete = (LinkButton)e.Row.Cells[10].FindControl("ibtnDelete");


            ibtnDelete.Attributes.Add("onClick", "javascript:return confirm('" + GetLanageValueByKey("lbl_OnlineUser_DelMsg", PowerCreator.LMS.Language.LanagetItemType.Page) + "');");

            e.Row.Attributes.Add("bgColor", "#F7FAFF");
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#FAEDB8'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#F7FAFF'");




            ibtnEditor.Text = GetLanageValueByKey("btn_ManagerQuestion_Edit", PowerCreator.LMS.Language.LanagetItemType.Page);
            ibtnPreview.Text = GetLanageValueByKey("btn_ManagerPaperInfo_ViewPaper", PowerCreator.LMS.Language.LanagetItemType.Page);
            ibtnDelete.Text = GetLanageValueByKey("btn_ManagerPaperInfo_Del", PowerCreator.LMS.Language.LanagetItemType.Page);
            btn_Out.Text = GetLanageValueByKey("btn_ManagerPaperInfo_OutWord", PowerCreator.LMS.Language.LanagetItemType.Page);


            if (e.Row.Cells[7].Text == "0")
            {
                e.Row.Cells[7].Text = GetLanageValueByKey("lab_AddPaperInfo_CommonPaper", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
            else
            {
                e.Row.Cells[7].Text = GetLanageValueByKey("lab_AddPaperInfo_TestPaper", PowerCreator.LMS.Language.LanagetItemType.Page);
            }

            if (e.Row.Cells[9].Text == "0")
            {
                e.Row.Cells[9].Text = GetLanageValueByKey("Msg_ManagerPaperInfo_NoBuild", PowerCreator.LMS.Language.LanagetItemType.Page);
                ibtnPreview.Enabled = false;
                btn_Out.Enabled = false;
            }
            else
            {
                e.Row.Cells[9].Text = GetLanageValueByKey("Msg_ManagerPaperInfo_Build", PowerCreator.LMS.Language.LanagetItemType.Page);
                ibtnPreview.Enabled = true;
                btn_Out.Enabled = true;
            }

            //if (dpdlst_PaperType.SelectedValue == "1")
            //{
            //    btn_SetUser.Visible = true;
            //}
            //else
            //{
            //    btn_SetUser.Visible = false;
            //}
        } 
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {

    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        //删除选中
        for (int i = 0; i < gvPaperList.Rows.Count; i++)
        {
            CheckBox cbox = (CheckBox)gvPaperList.Rows[i].Cells[0].FindControl("chkSelect");
            if (cbox.Checked)
            {
                Label lab_PaperID = (Label)gvPaperList.Rows[i].Cells[0].FindControl("lab_PaperID");
                string paperID = lab_PaperID.Text;


                //检查是否有方案引用
                if (objProjectResource.QueryProjectResourceCount(" FKProresPaperID=" + paperID) > 0)
                {
                    Script.Alert(this, "msg", "试卷被方案引用，请先删除引用该试卷的方案");
                    return;
                }


                #region 删除试卷信息表和试卷试题表相关记录

                objManagerPaper.DeletePaper(int.Parse(paperID));                               //删除试卷信息表
                string cmd = "FKPaperQuestionPaperID=" + paperID.ToString();
                DataTable dt_pqinfo = new DataTable();
                dt_pqinfo = objManagerPaper.QueryPaperQuestion(cmd);                       //获取试卷试题信息表相关记录
                for (int j = 0; j < dt_pqinfo.Rows.Count; j++)
                {
                    int PaperQuestionID = int.Parse(dt_pqinfo.Rows[j]["PaperQuestionID"].ToString());
                    objManagerPaper.DeletePaperQuestion(PaperQuestionID);                      //删除试卷试题信息表记录
                }
                #endregion

                #region 删除用户试卷答案表相关记录
                DataTable dt_paperuseranswer = new DataTable();
                dt_paperuseranswer = objManagerPaper.QueryPaperUserAnswer("FKPUanswerPaperID=" + paperID.ToString());
                for (int j = 0; j < dt_paperuseranswer.Rows.Count; j++)
                {
                    int PUanswerID = int.Parse(dt_paperuseranswer.Rows[j]["PUanswerID"].ToString());
                    objManagerPaper.DeletePaperUserAnswer(PUanswerID);
                }
                #endregion
            }
        }
        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, AspNetPager1.CurrentPageIndex);
    }

    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选 / 取消
        for (int i = 0; i < gvPaperList.Rows.Count; i++)
        {
            CheckBox cbox = (CheckBox)gvPaperList.Rows[i].Cells[0].FindControl("chkSelect");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //反选
        for (int i = 0; i < gvPaperList.Rows.Count; i++)
        {
            CheckBox cbox = (CheckBox)gvPaperList.Rows[i].Cells[0].FindControl("chkSelect");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderGV(search, AspNetPager1.CurrentPageIndex);
    }


    protected void btn_SortSearch_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "0";
        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, 1);
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {

        //string sPage = e.SortExpression;
        //if (ViewState["SortOrder"].ToString() == sPage)
        //{
        if (ViewState["OrderDire"].ToString() == "Desc")
            ViewState["OrderDire"] = "ASC";
        else
            ViewState["OrderDire"] = "Desc";
        //}
        //else
        //{
        ViewState["SortOrder"] = e.SortExpression;
        //}

        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, 1);
    }
}
